<template>
  <div class="login-container">
    <div class="login-bg" />
    <div class="login-content">
      <div class="login-card">
        <div class="login-header">
          <div class="logo-section">
            <el-icon class="logo-icon"><User /></el-icon>
            <h1 class="title">管理系统</h1>
            <p class="subtitle">欢迎登录</p>
          </div>
        </div>

        <div class="login-form">
          <el-form 
              :model="form" 
              :rules="formRules" 
              ref="formRef" 
              label-position="top"
              class="form"
            >
            <el-form-item label="用户名" prop="username">
              <el-input 
                v-model="form.username" 
                placeholder="请输入用户名" 
                size="large"
                prefix-icon="User"
                clearable
                :disabled="loginStore.isLoading"
              />
            </el-form-item>

            <el-form-item label="密码" prop="password">
              <el-input 
                v-model="form.password" 
                type="password" 
                placeholder="请输入密码" 
                size="large"
                prefix-icon="Lock"
                show-password
                :disabled="loginStore.isLoading"
              />
            </el-form-item>

            <el-form-item>
              <el-button 
                class="submit-btn"
                type="primary" 
                size="large"
                @click="handleLogin" 
                :loading="loginStore.isLoading"
              >
                <el-icon><Right /></el-icon>
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { User, Right, Lock } from '@element-plus/icons-vue'
import { useLoginStore } from '@/stores/login/loginStore'
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

// 状态定义
const loginStore = useLoginStore()
const formRef = ref(null)

// 表单数据
const initialFormData = {
  username: 'admin',
  password: '123456'
}

const form = reactive({ ...initialFormData })

// 表单验证规则
const formRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

// 处理登录
const handleLogin = async () => {
  if (!formRef.value) {
    ElMessage.error('表单引用未初始化')
    return
  }
  
  // 进行表单验证
  const valid = await formRef.value.validate()
  if (!valid) {
    ElMessage.error('请填写完整的登录信息')
    return
  }
  
  // 验证通过后调用store的登录方法
  await loginStore.handleLogin(form.username, form.password)
}
</script>
 
<style lang="scss" scoped>
@use '@/styles/variables.scss' as *;

// 登录容器
.login-container {
  position: relative;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, $gray-100 0%, $white 100%);
}

// 背景装饰
.login-bg {
  position: absolute;
  inset: 0;
  background-image: 
    radial-gradient(ellipse at 20% 20%, rgba(64, 158, 255, 0.08), transparent 50%),
    radial-gradient(ellipse at 80% 0%, rgba(103, 194, 58, 0.08), transparent 50%),
    radial-gradient(ellipse at 60% 80%, rgba(255, 153, 0, 0.07), transparent 50%);
  pointer-events: none;
}

// 登录内容区域
.login-content {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 400px;
  padding: 20px;
}

// 登录卡片
.login-card {
  @include card-style;
  padding: 40px 32px;
  text-align: center;

  .login-header {
    margin-bottom: 32px;

    .logo-section {
      .logo-icon {
        font-size: 48px;
        color: $primary-color;
        margin-bottom: 16px;
      }

      .title {
        font-size: 24px;
        font-weight: 600;
        color: $text-color;
        margin: 0 0 8px 0;
      }

      .subtitle {
        font-size: 14px;
        color: $text-color-secondary;
        margin: 0;
      }
    }
  }

  .login-form {
    .form {
      .submit-btn {
        width: 100%;
        height: 44px;
        font-size: 16px;
        font-weight: 600;
        @include button-style;
      }
    }
  }
}

// Element Plus 组件样式覆盖
:deep(.el-form-item) {
  margin-bottom: 20px;

  .el-form-item__label {
    font-weight: 500;
    color: $text-color;
    font-size: 14px;
    margin-bottom: 8px;
  }
}

// 使用Element Plus默认样式，不进行自定义覆盖

:deep(.el-button) {
  @include button-style;

  &.el-button--primary {
    background: linear-gradient(135deg, $primary-color, #337ecc);
    border: none;

    &:hover {
      background: linear-gradient(135deg, #337ecc, #2c5aa0);
      transform: translateY(-1px);
      box-shadow: 0 4px 12px rgba(64, 158, 255, 0.3);
    }
  }
}

// 响应式设计
@media (max-width: 480px) {
  .login-content {
    padding: 16px;
  }

  .login-card {
    padding: 24px 20px;
  }
}
</style>
    